//
//  HZCartListVCModel.m
//  app
//
//  Created by 刘华舟 on 15/5/5.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZCartListVCModel.h"
#import "HZCartCellModel.h"
#import "HZCartTotalModel.h"
#import "HZRemoteResult.h"

#import "HZMallSqlDataManager.h"

@implementation HZCartListVCModel

-(id)initWithJSONData:(NSDictionary*)data{
    if (self = [super init]) {
        _productsNum = 0;
        
        HZMallSqlDataManager *sqlDataManager = [HZMallSqlDataManager defaultManager];
        
        NSMutableArray *cellModelArray = [NSMutableArray array];
        for (NSDictionary *dict in [data arrayForKey:APIKEY_mall_result]) {
            HZCartCellModel *model = [HZCartCellModel modelWithDictionary:dict type:HZCartTableViewCellTypeShoppingCart];
            
            model.selected = [sqlDataManager queryProductChoseStatuWithProductID:model.goodsID];
            
            [cellModelArray addObject:model];
            _productsNum += [model.goodsCountNum integerValue];
            
        }
        self.dataArray = cellModelArray;
        self.noMoreData = NO;
        
        self.panelModel = [[HZCartTotalModel alloc] initWithMoney:@"0.00" num:@"0" selectedAll:NO];
        
        [self fillPanelModel];
    }
    
    return self;
}

- (void)fillPanelModel{
    CGFloat totalMoney = 0.00;
    NSInteger totalCount = 0;
    BOOL selectedAll = YES;
    
    if (self.dataArray.count <= 0) {
        selectedAll = NO;
        self.noMoreData = YES;
    }
    
    for (HZCartCellModel *model in self.dataArray) {
        if (!model.isSelected) {
            selectedAll = NO;
        }else{
            //计算选中商品个数
            totalCount += [model.goodsCountNum integerValue];
            //计算总价格
            totalMoney += ([model.goodsPriceNow floatValue] * [model.goodsCountNum integerValue]);
        }
    }
    
    self.panelModel.totalMoney = [NSString stringWithFormat:@"%0.2f", totalMoney];
    self.panelModel.totalGoodsNum = [NSString stringWithFormat:@"%ld", (long)totalCount];
    self.panelModel.allSelected = selectedAll;
    
}

- (void)choseAllModelWithFlag:(BOOL)isChoseAll{
    
    if (isChoseAll) {
        CGFloat totalMoney = 0.00;
        NSInteger totalCount = 0;
        BOOL selectedAll = YES;
        
        if (self.dataArray.count <= 0) {
            selectedAll = NO;
            self.noMoreData = YES;
        }
        
        for (HZCartCellModel *model in self.dataArray) {
            model.selected = YES;
            
            if (!model.isSelected) {
                selectedAll = NO;
            }else{
                //计算选中商品个数
                totalCount += [model.goodsCountNum integerValue];
                //计算总价格
                totalMoney += ([model.goodsPriceNow floatValue] * [model.goodsCountNum integerValue]);
            }
        }
        
        self.panelModel.totalMoney = [NSString stringWithFormat:@"%0.2f", totalMoney];
        self.panelModel.totalGoodsNum = [NSString stringWithFormat:@"%ld", (long)totalCount];
        self.panelModel.allSelected = selectedAll;
    }else{
        CGFloat totalMoney = 0.00;
        NSInteger totalCount = 0;
        BOOL selectedAll = YES;
        
        if (self.dataArray.count <= 0) {
            selectedAll = NO;
            self.noMoreData = YES;
        }
        
        for (HZCartCellModel *model in self.dataArray) {
            
            model.selected = NO;
            
            selectedAll = NO;
        }
        
        self.panelModel.totalMoney = [NSString stringWithFormat:@"%0.2f", totalMoney];
        self.panelModel.totalGoodsNum = [NSString stringWithFormat:@"%ld", (long)totalCount];
        self.panelModel.allSelected = selectedAll;
    }
    
    
}

-(HZCartCellModel *)fetchCellModelAtRow:(NSInteger)row{
    return [self.dataArray objectAtIndex:row];
}

//删除cellModel
- (void)deleteCellModel{
    if (self.cellModelNeedToDelete) {
        NSMutableArray *dataArray = [NSMutableArray arrayWithArray:self.dataArray];
        
        self.cellModelNeedToDelete.selected = NO;
        
        [dataArray removeObject:self.cellModelNeedToDelete];
        self.dataArray = dataArray;
        self.cellModelNeedToDelete = nil;
        [self fillPanelModel];
    }
}

@end
